import { PrismaClient } from "@prisma/client";
import csvParser from "csv-parser";
import fs from "fs";
const prisma = new PrismaClient();

async function main() {
  await prisma.dataset.deleteMany();
  await prisma.testResult.deleteMany();
  await initDataset();
  const stdDataset = await prisma.dataset.findUnique({
    where: {
      name: "standard",
    },
  });
  await prisma.testResult.create({
    data: {
      datasetId: stdDataset.id,
      indicator1: 0.9503,
      indicator2: 0.6806,
    },
  });
  await initGen();
  await initRob();
}

main()
  .then(() => prisma.$disconnect())
  .catch((e) => {
    console.error(e);
    prisma.$disconnect();
    process.exit(1);
  });

async function initDataset() {
  const datasetPath = "../app/dataset";
  const nameList = fs.readdirSync(datasetPath);
  nameList.sort((a, b) => parseInt(a.slice(4)) - parseInt(b.slice(4)));
  for (let name of nameList) {
    const nameStr = name.split("_");
    await prisma.dataset.create({
      data: {
        name: name,
        origin: nameStr[0],
        noise: nameStr[2] ? parseFloat(nameStr[2]) : 0,
        standard: nameStr[0] === "standard",
      },
    });
  }
}

async function initGen() {
  const results = [];
  fs.createReadStream("./resultcsv-fanhua.csv")
    .pipe(csvParser())
    .on("data", (row) => {
      results.push(row);
    })
    .on("end", async () => {
      for (let index = 0; index < results.length; index++) {
        const dataset = await prisma.dataset.findUnique({
          where: {
            name: `test${index + 1}`,
          },
        });
        await prisma.testResult.create({
          data: {
            datasetId: dataset.id,
            indicator1: parseFloat(results[index]["mAP_0.5"]),
            indicator2: parseFloat(results[index]["mAP_50_95"]),
          },
        });
      }
    });
}

async function initRob() {
  const noise = [
    1, 2, 3, 4, 5, 6, 7, 8, 8.95, 9, 9.05, 9.1, 9.15, 9.2, 9.25, 9.3, 9.35, 9.4,
    9.45, 9.5, 9.55, 9.6, 9.65, 9.7, 9.75, 9.8, 9.85,
  ];
  for (let groupNum = 1; groupNum <= 99; groupNum++) {
    const results = [];
    fs.createReadStream(`./lubang${groupNum}.csv`)
      .pipe(csvParser())
      .on("data", (row) => {
        results.push(row);
      })
      .on("end", async () => {
        for (let index = 0; index < results.length; index++) {
          const dataset = await prisma.dataset.findUnique({
            where: {
              name: `test${groupNum}_noise_${noise[index] * 2}`,
            },
          });
          await prisma.testResult.create({
            data: {
              datasetId: dataset.id,
              indicator1: parseFloat(results[index]["mAP_0.5"]),
              indicator2: parseFloat(results[index]["mAP_50_95"]),
            },
          });
        }
      });
  }

  const results = [];
  fs.createReadStream(`./lubang1000.csv`)
    .pipe(csvParser())
    .on("data", (row) => {
      results.push(row);
    })
    .on("end", async () => {
      for (let index = 0; index < results.length; index++) {
        const dataset = await prisma.dataset.findUnique({
          where: {
            name: `standard_noise_${noise[index] * 2}`,
          },
        });
        await prisma.testResult.create({
          data: {
            datasetId: dataset.id,
            indicator1: parseFloat(results[index]["mAP_0.5"]),
            indicator2: parseFloat(results[index]["mAP_50_95"]),
          },
        });
      }
    });
}
